#include	"unp.h"
#include 	"ft_info.h"

int sockfd;

void *stdin_do(void *arg);
void *sockin_do(void *arg);
void *file_receiver(void *arg);

int main(int argc, char **argv) {

	int listenfd, sockfd, connfd;//, *iptr;
	pthread_t tid;
	socklen_t addrlen, len;
	struct sockaddr *cliaddr;

	char port_num[10];

	strcpy(port_num, "9866");

	if (argc != 3)
		err_quit("usage: tcpcli <hostname> <service>");

	sockfd = Tcp_connect(argv[1], argv[2]);

	listenfd = Tcp_listen(NULL, port_num, &addrlen);

	printf("Client START\n> ");
	Pthread_create(&tid, NULL, &stdin_do, (void *) sockfd);
	Pthread_create(&tid, NULL, &sockin_do, (void *) sockfd);

	cliaddr = malloc(addrlen);

	for (;;) {
		len = addrlen;
		cliaddr = malloc(addrlen);
		//iptr = Malloc(sizeof(int));
		//*iptr = Accept(listenfd, cliaddr, &len);
		if((connfd = accept(listenfd, cliaddr, &len)) < 0){
			printf("accept eror accept errororororo!!\n");

		}

		//connfd = accept(listenfd, cliaddr, &len);
		receiv_file(connfd);
		//pthread_create(&tid, NULL, &file_receiver, iptr);

	}

	exit(0);
}
